package utils

import (
	"fmt"
	"github.com/gogf/gf/frame/g"
	"github.com/tebeka/selenium"
	"github.com/tebeka/selenium/chrome"
)

func GetWebDriver(port int) (*selenium.Service, selenium.WebDriver, error) {

	opts := []selenium.ServiceOption{}

	caps := selenium.Capabilities{

		"browserName": "chrome",
	}

	// 禁止加载图片，加快渲染速度

	imagCaps := map[string]interface{}{

		//"profile.managed_default_content_settings.images": 2,
		"acceptInsecureCerts": true,
		"acceptSslCerts":      true,
	}

	chromePath := g.Cfg().GetString("chromedriver.chrome_path")

	chromeCaps := chrome.Capabilities{

		Prefs: imagCaps,

		Path: chromePath,

		Args: []string{

			//"--headless", // 设置Chrome无头模式
			"--disable-gpu",
			"--no-sandbox",

			"--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7", // 模拟user-agent，防反爬
			"--ignore-certificate-error",
			"--ignore-ssl-errors",
			"--enable-javascript",
		},
	}

	caps.AddChrome(chromeCaps)
	// 启动chromedriver，端口号可自定义
	driverPath := g.Cfg().GetString("chromedriver.path")

	service, err := selenium.NewChromeDriverService(driverPath, port, opts...)
	if err != nil {
		fmt.Printf("Error starting the ChromeDriver server: %v", err)
		return nil, nil, err
	}

	webDriver, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
	if err != nil {
		fmt.Printf("Error selenium.NewRemote server: %v", err)
		return nil, nil, err
	}

	return service, webDriver, nil
}

// GetWebDriverDownload 点击下载按钮的webdriver
func GetWebDriverDownload(port int, dir string) (*selenium.Service, selenium.WebDriver, error) {

	opts := []selenium.ServiceOption{}

	caps := selenium.Capabilities{

		"browserName": "chrome",
	}

	imagCaps := map[string]interface{}{

		//"profile.managed_default_content_settings.images": 2,    // 禁止加载图片，加快渲染速度,拿的是页面标签的图片
		"acceptInsecureCerts":                     true, //接受不安全证书
		"acceptSslCerts":                          true,
		"profile.default_content_settings.popups": 0,   //禁止弹窗
		"download.default_directory":              dir, //设置下载路径
	}

	chromePath := g.Cfg().GetString("chromedriver.chrome_path")
	chromeCaps := chrome.Capabilities{

		Prefs: imagCaps,

		Path: chromePath,

		Args: []string{

			//"--headless", // 设置Chrome无头模式

			"--no-sandbox",
			"--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7", // 模拟user-agent，防反爬
			"--ignore-certificate-error",
			"--ignore-ssl-errors",
			"--disable-gpu",
			"--disable-features=OverwriteText", // 下载文件时覆盖之前的文件
			"--log-level=3",                    // 禁用掉 DevTools listening 输出

		},
	}

	caps.AddChrome(chromeCaps)
	// 启动chromedriver，端口号可自定义
	driverPath := g.Cfg().GetString("chromedriver.path")

	service, err := selenium.NewChromeDriverService(driverPath, port, opts...)
	if err != nil {
		fmt.Printf("Error starting the ChromeDriver server: %v", err)
		return nil, nil, err
	}

	webDriver, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
	if err != nil {
		fmt.Printf("Error selenium.NewRemote server: %v", err)
		return nil, nil, err
	}

	return service, webDriver, nil
}
